import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";
import type { ClusterRoleList, RoleBinding, RoleBindingList } from "./types";

export class K8sClusterRole {
  clusterRoleUrl(cluster_name) {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/rbac.authorization.k8s.io/v1/clusterroles`;
  }

  listClusterRoles(
    cluster_name?: string,
    keywords?: string,
    pageNum?: number,
    pageSize?: number
  ): BaseResponse<ClusterRoleList> {
    let url = this.clusterRoleUrl(cluster_name);
    const params = {};
    if (keywords) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  deleteClusterRole(cluster_name, name) {
    return http.delete(`${this.clusterRoleUrl(cluster_name)}/${name}`);
  }

  getClusterRole(cluster_name, name) {
    return http.get(`${this.clusterRoleUrl(cluster_name)}/${name}`);
  }

  createClusterRole(cluster_name, data) {
    return http.post(`${this.clusterRoleUrl(cluster_name)}`, data);
  }

  updateClusterRole(cluster_name, name, data) {
    return http.put(`${this.clusterRoleUrl(cluster_name)}/${name}`, data);
  }
}

export class K8sClusterRoleBinding {
  clusterRoleBindingUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/rbac.authorization.k8s.io/v1/clusterrolebindings`;
  };

  listClusterRoleBindings(
    cluster_name?: string,
    keywords?: string,
    pageNum?: number,
    pageSize?: number
  ): BaseResponse<RoleBindingList> {
    let url = this.clusterRoleBindingUrl(cluster_name);
    const params = {};
    if (keywords) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  deleteClusterRoleBinding(cluster_name, name) {
    return http.delete(`${this.clusterRoleBindingUrl(cluster_name)}/${name}`);
  }

  getClusterRoleBinding(cluster_name, name) {
    return http.get(`${this.clusterRoleBindingUrl(cluster_name)}/${name}`);
  }

  createClusterRoleBinding(cluster_name, data) {
    return http.post(`${this.clusterRoleBindingUrl(cluster_name)}`, data);
  }

  updateClusterRoleBinding(cluster_name, name, data) {
    return http.put(
      `${this.clusterRoleBindingUrl(cluster_name)}/${name}`,
      data
    );
  }
}
